﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EasyKaPai
{
    public class KeyboardInfo
    {
        private KeyboardInfo() { }
        [DllImport("user32")]
        private static extern short GetKeyState(int vKey);
        public static KeyStateInfo GetKeyState(Keys key)
        {
            int vkey = (int)key;
            if (key == Keys.Alt)
            {
                vkey = 0x12;    // VK_ALT
            }
            short keyState = GetKeyState(vkey);
            byte[] bits = BitConverter.GetBytes(keyState);
            bool toggled = bits[0] > 0, pressed = bits[1] > 0;
            return new KeyStateInfo(key, pressed, toggled);
        }
    }

    public struct KeyStateInfo
    {
        Keys m_key;
        bool m_isPressed,
             m_isToggled;
        public KeyStateInfo(Keys key,
                        bool ispressed,
                        bool istoggled)
        {
            m_key = key;
            m_isPressed = ispressed;
            m_isToggled = istoggled;
        }
        public static KeyStateInfo Default
        {
            get
            {
                return new KeyStateInfo(Keys.None, false, false);
            }
        }
        public Keys Key
        {
            get { return m_key; }
        }
        public bool IsPressed
        {
            get { return m_isPressed; }
        }
        public bool IsToggled
        {
            get { return m_isToggled; }
        }
    }
}